﻿using _111.Models;
using _111.QywxDocument.WeeklyReport.Model;
using Google.Protobuf.WellKnownTypes;
using guidHelper;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using MyDapperApi.Models;
using MyDapperApi.Services.BaiLianAI;
using MyDapperApi.Services.QywxBase;
using Mysqlx.Crud;
using Newtonsoft.Json;
using Org.BouncyCastle.Ocsp;
using QYWXUpdate.DataModel;
using QYWXUpdate.DataModel.CheckInfo;
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using static Dapper.SqlMapper;
using static MyDapperApi.Services.DateBaseService;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace MyDapperApi.Services.Mbti;

public class MbtiService : IMbtiService
{
    private readonly IConfiguration _configuration;
    private readonly IDateBaseService _dateBaseService;
    private readonly IHttpClientFactory _httpClientFactory;
    private readonly IBaiLianAIBaseService _biLianAIBaseService;
    private readonly IQywxBaseService _iqywxBaseService;

    public MbtiService(IConfiguration configuration,
        IDateBaseService dateBaseService,
        IHttpClientFactory httpClientFactory,
        IBaiLianAIBaseService biLianAIBaseService,
        IQywxBaseService qywxBaseService)
    {
        _configuration = configuration;
        _dateBaseService = dateBaseService;
        _httpClientFactory = httpClientFactory;
        _biLianAIBaseService = biLianAIBaseService;
        _iqywxBaseService = qywxBaseService;
    }

    /// <summary>
    /// 用来保存mbti结果数据的方法
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<CommonResponse> SaveMbtiResult(SaveMbtiRequest request)
    {
        var result = new CommonResponse();

        var entity = await CheckEntity(request);

        result = await SaveMbti(entity);

        return result;
    }

    public async Task<MbtiResultEntity> CheckEntity(SaveMbtiRequest request)
    {
        var result = new MbtiResultEntity()
        {
            Name = request.Name,
            Phone = request.Phone,
            Result = request.Result,
            Is_Deleted = 0
        };

        var sql = string.Empty;
        var sqlBuilder = new SqlBuilder();
        sqlBuilder.Add(" AND phone = @Phone ","@Phone", request.Phone);
        sqlBuilder.Add(" AND name = @Name ","@Name", request.Name);

        sql = $@"SELECT * FROM mbti_result WHERE 1 = 1 AND is_deleted = 0 ";

        var entity = await _dateBaseService.QueryFirstOrDefaultAsync<MbtiResultEntity>(sql,sqlBuilder,0, "MiniServer");
        if (entity == null)
        {
            result.Created_On = DateTime.Now;
        }
        else
        {
            result.Id=entity.Id;
            result.Modified_On = DateTime.Now;
        }

        return result;
    }

    /// 保存基础数据到数据库
    /// </summary>
    /// <param name="request"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public async Task<CommonResponse> SaveMbti(MbtiResultEntity request)
    {
        var result = new CommonResponse();
        var sql = string.Empty;
        var sqlbuilder = new SqlBuilder();
        sqlbuilder.Add("", "@IsDeleted", request.Is_Deleted);
        sqlbuilder.Add("", "@Name", request.Name);
        sqlbuilder.Add("", "@Phone", request.Phone);
        sqlbuilder.Add("", "@Result", request.Result);

        if (string.IsNullOrEmpty(request.Id))
        {
            request.Id = ToolHelper.CommonId();
            sql = @$"INSERT INTO mbti_result (
    id, 
    created_on, 
    is_deleted, 
    name,
    phone,
    result
) VALUES (
    @Id, 
    @CreatedOn, 
    @IsDeleted, 
    @Name, 
    @Phone, 
    @Result
);";
            sqlbuilder.Add("", "@CreatedOn", request.Created_On);
        }
        else
        {
            sql = $@"UPDATE mbti_result
SET 
    modified_on = @ModifiedOn,        
    is_deleted = 0,        
    name = @Name,    
    phone = @Phone,
    result = @Result
WHERE 
    id = @Id;";
            sqlbuilder.Add("", "@ModifiedOn", request.Modified_On);
        }

        sqlbuilder.Add("", "@Id", request.Id);

        result = await _dateBaseService.ExecuteAsync(sql, sqlbuilder, 0, "MiniServer");

        return result;
    }
}